package net.ibizsys.eclipse.studio.dialog;

import net.ibizsys.eclipse.studio.Activator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * 对话框基类
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class BaseViewDialog extends Dialog
{
	protected boolean bBusyLoading = false;
	protected Label infoLabel = null;

	public BaseViewDialog(Shell parentShell)
	{
		super(parentShell);
	}

	/**
	 * 提示错误信息
	 * 
	 * @param title
	 * @param info
	 */
	protected void alert(final String title, final String info)
	{
		runOnUIThread(new Runnable()
		{
			@Override
			public void run()
			{
				if (infoLabel != null)
				{
					infoLabel.setText(info);
				}
			}
		});
	}

	/**
	 * 显示遮罩
	 */
	protected void showHUD(final String hudText)
	{
		bBusyLoading = true;

		runOnUIThread(new Runnable()
		{
			@Override
			public void run()
			{
				if (infoLabel != null)
				{
					infoLabel.setText(hudText);
				}
			}
		});
	}

	/**
	 * 隐藏遮罩
	 */
	protected void hideHUD()
	{
		bBusyLoading = false;
		runOnUIThread(new Runnable()
		{
			@Override
			public void run()
			{
				if (infoLabel != null)
				{
					infoLabel.setText("");
				}
			}
		});
	}

	/**
	 * 是否正在忙碌
	 * 
	 * @return
	 */
	protected boolean isBusy()
	{
		return bBusyLoading;
	}

	/**
	 * 在UI线程执行
	 * 
	 * @param runable
	 */
	protected void runOnUIThread(Runnable runable)
	{
		Activator.getDefault().getWorkbench().getDisplay().syncExec(runable);
	}
}
